package initialize

import (
	"fmt"
	"github.com/gin-gonic/gin/binding"
	"github.com/go-playground/locales/en"
	"github.com/go-playground/locales/zh"
	ut "github.com/go-playground/universal-translator"
	"github.com/go-playground/validator/v10"
	zhtranslations "github.com/go-playground/validator/v10/translations/zh"
	"mxshop-api/user-web/global"
)

func InitTrans() error {
	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
		zhT := zh.New()
		enT := en.New()
		uni := ut.New(enT, zhT)
		// 第一个参数是备用的语言环境，第二参数是应该支持的语言环境
		global.Trans, ok = uni.GetTranslator("zh")
		if !ok {
			return fmt.Errorf("uni.GetTranslator")
		}
		if err := zhtranslations.RegisterDefaultTranslations(v, global.Trans); err != nil {
			return err
		}
	}
	return nil
}
